<?php
namespace backend\controllers;

use backend\models\PasswordForm;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends BaseController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index','password'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                   // 'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        // 禁用函数
        $disableFunctions = ini_get('disable_functions');
        $disableFunctions = !empty($disableFunctions) ? explode(',', $disableFunctions) : '未禁用';
        return $this->render('info', [
            'disable_functions' => $disableFunctions,
        ]);

        //return $this->render('index');
    }


    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {

        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) ) {
            if(!$model->login()){
                Yii::$app->session->setFlash('error', "用户名密码错误");
            }
            return $this->goBack();
        } else {
            $model->password = '';
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }


    public function actionPassword()
    {
        $model = new PasswordForm();
        if ($model->load(Yii::$app->request->post()) ) {
         if($model->validate()){
             if(!$model->Uppassword()){
                 Yii::$app->session->setFlash('error', "密码修改失败");
             }else{
                 Yii::$app->session->setFlash('success', "密码修改成功，重新登陆");
                 Yii::$app->user->logout();
             }
         }else{
                  Yii::$app->session->setFlash('error', $model->getErrorSummary(false)[0]);
         }

            return $this->redirect('password');
        } else {
            return $this->render('password', [
                'model' => $model,
            ]);
        }

    }


}
